<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/09/07
 * Time: 18:35
 */

use Illuminate\Support\Facades\Redis;
class AhpArticleController extends BaseController {

    /**
     * @Author:      sqq
     * @DateTime:    2018-09-07 18:36:28
     * @Description: 添加文章/修改文章
     * @param  $token   用户token
     * @link: http://www.activity.com/ahp/ahp3.0/article/addArticle?token=899a085c1fa316ba14649c1ec7e26a73&id=8&title=1&keyword[]=1,2&article_type=1&first_image=1&list_image=2&share_image=3&form_set=[[{%22widget_type%22:1,%22widget_title%22:%22%E7%94%B3%E8%AF%B7%E4%BA%BA%22,%22widget_sort%22:1,%22widget_desc%22:%22111111111%22}]&share_name=1111&share_desc=2222
     */
     public function add_article(){
         $validator = Validator::make(Input::all(),
             array(
                 'token' => 'r equired',
                 'title' => 'required',
                 'keyword' => 'required',
                 'article_type' => 'required',
                 'first_image' => 'required',
                 'list_image' => 'required',
                 'share_image' => 'required',
                 'form_set' => 'required',
                 'share_name' => 'required',
                 'share_desc' => 'required',
             )
         );
         $response = array('code' => 0, 'msg' => 'ok');
         do{
             if($validator->fails()) {
                 $response = Lang::get("ahp_error.args_error_1000");
                 $response["result"]=$validator->messages()->all();
                 break;
             }
             $users=$this->verificationAhpUserToken(Input::get("token"),$response);
             if(is_null($users)){
                 break;
             }

             //获取参数
             $id = Input::get('id',0); //文章id  没有传0
             $title = Input::get('title');
             $keyword = Input::get('keyword');
             $article_type = Input::get('article_type');
             $first_image = Input::get('first_image');
             $list_image = Input::get('list_image');
             $share_image = Input::get('share_image');
             $form_set = Input::get('form_set');
             $share_name = Input::get('share_name');
             $share_desc = Input::get('share_desc');

             //判断数据格式是否正确

             if(!is_array(json_decode($form_set, true))){
                 $response = Lang::get("ahp_error.ahp_article_not_json_40000");
                 break;
             }

//             if(!is_array($keyword)){
//                 $response = Lang::get("ahp_error.ahp_article_not_array_40008");
//                 break;
//             }
             if(!is_array(json_decode($keyword, true))){
                 $response = Lang::get("ahp_error.ahp_article_not_array_40008");
                 break;
             }

             $article = ArticleWechat3::where('id',$id)->first();
             //存在就修改，不存在就添加
             if(!$article) {
                 $article = new ArticleWechat3();
                 $article->created_by = $users->id;
             }
             $article->updated_by = $users->id;
             $article->updated_at = Common::currentTimeString();
             if($title){
                 $article->title = $title;
             }
             if($keyword){
                 $keyword = json_decode($keyword, true);
                 $keyword = array_column($keyword, 'id');
                 if(count($keyword) != count(array_unique($keyword))){
                     $response = Lang::get('ahp_error.wechat3_article_used_unique_keyword_40014');
                     break;
                 }
                 $keyword = implode(",", $keyword);
                 $article->keyword_id = $keyword;
             }
             if($article_type){
                 $article->article_type = $article_type;
             }
             if($first_image){
                 $article->first_image = $first_image;
             }
             if($list_image){
                 $article->list_image = $list_image;
             }
             if($share_image){
                 $article->share_image = $share_image;
             }
             if($form_set){
                 $article->form_set = $form_set;
             }
             try{
                 $article->save();

                 $keyword_id = array_filter(explode(',',$article->keyword_id));
                 ArticleKeyWordRelationWechat3::where('article_id',$article->id)->delete();
                 if(!empty($keyword_id)){
                     foreach($keyword_id as $k){
                         $product = new ArticleKeyWordRelationWechat3();
                         $product->article_id = $article->id;
                         $product->keyword_id = $k;
                         $product->status = 1;
                         $product->created_at = Common::currentTimeString();
                         $product->updated_at = Common::currentTimeString();
                         $product->save();
                     }
                 }

                 //生成分享信息
                 $article_share = ArticleShareWechat3::where('article_id',$article->id)->first();
                 if(!$article_share){
                     $article_share = new ArticleShareWechat3();
                     $article_share->created_at = Common::currentTimeString();
                 }
                 $article_share->article_id = $article->id;
                 $article_share->name = $share_name;
                 $article_share->title = $share_name;
                 $article_share->wechat_url = Config::get('article_share_wechat3.wechat_url');
                 $article_share->share_url = Config::get('article_share_wechat3.share_url');
                 $article_share->desc = $share_desc;
                 $article_share->img_url = $share_image;
                 $article_share->type = 'link';
                 $article_share->status = 1;
                 $article_share->updated_at = Common::currentTimeString();
                 $article_share->save();

                 $url = Config::get('article_share_wechat3.article_wechat3_url')."?id=".$article->id."&type=2";
                 $response = array('code' => 0, 'msg' => 'ok', 'result' => array('id' => $article->id, 'url' => $url));
             }catch(Exception $e){
                 Log::error("error", array("system" => "ahp" ,"event" => "add_article", 'input' =>Input::all() ,"exception" => $e));
                 if($id == 0){
                     $response = Lang::get('ahp_error.ahp_article_add_article_40004');
                 }else{
                     $response = Lang::get('ahp_error.ahp_article_update_article_40005');
                 }
                 break;
             }
         }while(false);
         Log::info("info", array("system" => "ahp" ,"event" => "add_article", 'input' =>Input::all() ,"output" => $response));
         return json_encode($response);
     }

    /**
     * @Author:      sqq
     * @DateTime:    2018-09-07 18:36:28
     * @Description: 修改文章页面加载
     * @param  $token   用户token
     * @link: http://www.activity.com/ahp/ahp3.0/article/articleOnload?token=954fb617d682f174df5fd97df411e202&id=1
     */
    public function article_onload(){
        $validator = Validator::make(Input::all(),
            array(
                'token' =>'required',
                'id' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $users=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $id = Input::get('id');

            $article = ArticleWechat3::find($id);
            if(!$article){
                $response = Lang::get("ahp_error.ahp_article_not_exist_40001");
                break;
            }

            if(!is_null($article)){
                //查询已选择的关键词
                $keyword_id = explode(',',$article->keyword_id);
                $keyword = [];
                foreach($keyword_id as $val){
                    $re = ArticleKeywordWechat3::where('id',$val)->where('status',1)->select('id','keyword_name')->first();
                    if(!is_null($re)){
                        $re = $re->toArray();
                        array_push($keyword,$re);
                    }
                }
                $article->keyword = $keyword;
//                $article->source = explode(',',$article->source);
//                $r2 = '未投放';
//                if(in_array('1',$article->source)){
//                    $r2 = 'APP';
//                }
//                if(in_array('2',$article->source)){
//                    $r2 = '微站';
//                }
//                if(in_array('2',$article->source) && in_array('1',$article->source)){
//                    $r2 = 'APP/微站';
//                }
//                $article->source = $r2;
                //查询维护人
                $users_name = AhpUsers::find($article->updated_by);
                $article->real_name = $users_name->real_name;

                $share = ArticleShareWechat3::where('article_id',$id)->first();
                if(!is_null($share)){
                    $article->share_name = $share->title;
                    $article->share_desc = $share->desc;
                }else{
                    $article->share_name = '';
                    $article->share_desc = '';
                }
                $article->article_first_image_file_url = Config::get('article_share_wechat3.article_first_image_file_url');
                $article->article_list_image_file_url = Config::get('article_share_wechat3.article_list_image_file_url');
                $article->article_share_image_file_url = Config::get('article_share_wechat3.article_share_image_file_url');
                $article->article_wechat3_url = Config::get('article_share_wechat3.article_wechat3_url').'?id='.$id;
            }

            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $article
            );
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "article_onload", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-09-07 18:36:28
     * @Description: 文章列表
     * @param  $token   用户token
     * @link: http://www.activity.com/ahp/ahp3.0/article/articleList?token=899a085c1fa316ba14649c1ec7e26a73&title=2&article_type=1&is_recommend=0&source=0
     */
    public function articleList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $users=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $title = Input::get('title');
            $article_type = Input::get('article_type');
            $is_recommend = Input::get('is_recommend');
            $source = Input::get('source');
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);

            $result = new ArticleWechat3();
            if($title){
                $result = $result->where('title','LIKE','%'.$title.'%');
            }
            if($article_type){
                $result = $result->where('article_type',$article_type);
            }
            if($is_recommend){
                $result = $result->where('is_recommend',$is_recommend);
            }
            //if($source){
                $result = $result->where('source','LIKE','%'.$source.'%');
            //}
            $result = $result->orderBy('id','desc');
            $count = $result->count();
            $result=$result->skip($page_size*($page-1))->take($page_size)->get();

            if(!is_null($result)){
                foreach($result as $key => $val){
                    $val->source = explode(',',$val->source);
//                    $r2 = '未投放';
//                    if(in_array('1',$val->source)){
//                        $r2 = 'APP';
//                    }
//                    if(in_array('2',$val->source)){
//                        $r2 = '微站';
//                    }
//                    if(in_array('2',$val->source) && in_array('1',$val->source)){
//                        $r2 = 'APP/微站';
//                    }
                    $r2 = '0';
                    if(in_array('1',$val->source)){
                        $r2 = '1';
                    }
                    if(in_array('2',$val->source)){
                        $r2 = '1';
                    }
                    if(in_array('2',$val->source) && in_array('1',$val->source)){
                        $r2 = '1';
                    }
                    $val->source = $r2;

                    //查询维护人
                    $users_name = AhpUsers::find($val->updated_by);
                    if(!is_null($users_name)){
                        $val->real_name = $users_name->real_name;
                    }

                    //统计评论数
                    $val->comment = ArticleCommentWechat3::where('article_id',$val->id)->where('user_type','!=','2')->count();

                    $val->url = Config::get('article_share_wechat3.article_wechat3_url').'?id='.$val->id;
                }
            }

            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => [
                    'count' => $count,
                    'article_list' => $result
                ]
            );
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "articleList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-09-07 18:36:28
     * @Description: 文章投放
     * @param  $token String   用户token
     * @link: http://www.activity.com/ahp/ahp3.0/article/articleLaunch?token=954fb617d682f174df5fd97df411e202&id=1&source[]=2
     */
    public function articleLaunch(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=> 'required',
                //'source'=> 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $users = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $id = Input::get('id');
            $source = Input::get('source');

            //判断数据格式
            if($source && !is_array($source)){
                $response = Lang::get('ahp_error.ahp_article_not_array_40010');
                break;
            }

            try{
                ArticleLaunchWechat3::where('article_id',$id)->delete();
                $source = array_filter($source);
                if(!empty($source)){
                    foreach($source as $key => $val){
                        $launch = new ArticleLaunchWechat3();
                        $launch->article_id = $id;
                        $launch->url = Config::get('article_share_wechat3.article_wechat3_url').'?id='.$id.'&source='.$val;
                        $launch->source = $val;
                        $launch->created_at = Common::currentTimeString();
                        $launch->updated_at = Common::currentTimeString();
                        $launch->save();
                    }
                    $source = implode(',',$source);
                }else{
                    $source = '0';
                }
                ArticleWechat3::where('id',$id)->update(array('source' => $source, 'updated_by' => $users->id, 'updated_at' => Common::currentTimeString()));
            }catch(Exception $e){
                Log::error("error", array("system" => "ahp3.0" ,"event" => "articleKeyword", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get('ahp_error.ahp_article_lunach_not_exist_40011');
                break;
            }

            $response = array('code' => 0, 'msg' => 'ok');
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "articleKeyword", "input" =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-09-07 18:36:28
     * @Description: 推荐文章
     * @param  $token   用户token
     * @link: http://www.activity.com/ahp/ahp3.0/article/articleRecommend?token=899a085c1fa316ba14649c1ec7e26a73&id=4&is_recommend=1
     */
    public function articleRecommend(){
        $validator = Validator::make(Input::all(),
            array(
                'token' =>'required',
                'id' =>'required',
                'is_recommend' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $users=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $id = Input::get('id');
            $is_recommend = Input::get('is_recommend');

            //判断文章是否存在
            $article = ArticleWechat3::where('id',$id)->where('status',1)->first();
            if(!$article){
                $response = Lang::get("ahp_error.ahp_article_not_exist_40001");
                break;
            }

            //判断是否投放
            if($article->source == 0){
                $response = Lang::get("ahp_error.ahp_article_not_lunach_exist_40012");
                break;
            }

            if($article->is_recommend == $is_recommend && $is_recommend == 1){
                $response = Lang::get("ahp_error.ahp_article_recommended_40002");
                break;
            }

            if($article->is_recommend == $is_recommend && $is_recommend == 2){
                $response = Lang::get("ahp_error.ahp_article_not_recommended_40003");
                break;
            }
            try{
                $rs = ArticleWechat3::where('id',$id)->update(array('is_recommend' => $is_recommend, 'updated_at' => Common::currentTimeString(), 'updated_by' => $users->id));
            }catch(Exception $e){
                Log::error("error", array("system" => "ahp3.0" ,"event" => "articleRecommend", 'input' =>Input::all() ,"exception" => $e));
                if($is_recommend == 1){
                    $response = Lang::get('ahp_error.ahp_article_add_recommended_article_40006');
                }else{
                    $response = Lang::get('ahp_error.ahp_article_add_not_recommended_article_40007');
                }
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok');
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "articleRecommend", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-09-10 13:48:28
     * @Description: 文章分类
     * @param  $token   用户token
     * @link: http://www.activity.com/ahp/ahp3.0/article/articleType?token=899a085c1fa316ba14649c1ec7e26a73
     */
    public function articleType(){
        $validator = Validator::make(Input::all(),
            array(
                'token' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $users=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            $article_type = ArticleTypeWechat3::where('status',1)->select('id','type_name')->get();

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $article_type);
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "articleType", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-09-10 13:48:28
     * @Description: 文章--图片上传
     * @param  $token  String   用户token
     * @param  $file_type int   类型
     * @link:
     */
    public function articleUploadFile(){
        header("Content-Type: text/html; charset=utf-8");
        header('Access-Control-Allow-Origin:*');	//解决资源跨域问题
        header('Access-Control-Allow-Credentials', 'true');
        header('Access-Control-Allow-Headers', 'X-Requested-With,X_Requested_With');
        header('Access-Control-Allow-Headers', 'X-Requested-With, X_Requested_With');
        header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
        header('Access-Control-Allow-Credentials', 'true');
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'file'=> 'required',   //最大上传2M
                'file_type'=> 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $res = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($res)){
                break;
            }
            if ($res['status'] == 0) {
                $response = $res['response'];
                break;
            }

            $upload_file = Input::file('file');
            $file_type = Input::get('file_type');
            $file = [];
            if($upload_file->isValid()){
                //文件上传成功
                $name = $upload_file->getClientOriginalName();  //文件原名称
                $res = substr($name,strripos($name,".")+1);
                $storage_name = date('Ymdhis', time()).'.'.$res;               //存储名称
                switch ($file_type) {
                    case 'first_image':
                        $upload_file->move(Config::get('article_share_wechat3.article_first_image_file'), $storage_name);
                        $file['storage_name'] = Config::get('article_share_wechat3.article_first_image_file_url').$storage_name;
                        $file['url'] = Config::get('article_share_wechat3.article_first_image_file_url');
                        $file['name'] = $storage_name;
                        break;
                    case 'list_image':
                        $upload_file->move(Config::get('article_share_wechat3.article_list_image_file'), $storage_name);
                        $file['storage_name'] = Config::get('article_share_wechat3.article_list_image_file_url').$storage_name;
                        $file['url'] = Config::get('article_share_wechat3.article_list_image_file_url');
                        $file['name'] = $storage_name;
                        break;
                    case 'share_image':
                        $upload_file->move(Config::get('article_share_wechat3.article_share_image_file'), $storage_name);
                        $file['storage_name'] = Config::get('article_share_wechat3.article_share_image_file_url').$storage_name;
                        $file['url'] = Config::get('article_share_wechat3.article_share_image_file_url');
                        $file['name'] = $storage_name;
                        break;
                    case 'article_image':
                        $upload_file->move(Config::get('article_share_wechat3.article_share_image_file'), $storage_name);
                        $file['storage_name'] = Config::get('article_share_wechat3.article_share_image_file_url').$storage_name;
                        $file['url'] = Config::get('article_share_wechat3.article_share_image_file_url');
                        $file['name'] = $storage_name;
                        break;
                    default:
                        $response = Lang::get("ahp_error.ahp_file_error_30010");
                        break;
                }
            }else{
                $response = Lang::get("ahp_error.ahp_file_error_30010");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $file);
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "articleUploadFile", "input" =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-09-10 13:48:28
     * @Description: 文章--关键词使用
     * @param  $token  String   用户token
     * @link: http://www.activity.com/ahp/ahp3.0/article/articleKeyword?token=954fb617d682f174df5fd97df411e202&keyword=%E6%96%87%E7%AB%A0
     */
    public function articleKeyword(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'keyword'=> 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $users = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $keyword_name = Input::get('keyword');

            //判断是否存在
            $keyword = ArticleKeywordWechat3::where('keyword_name',$keyword_name)->select('id','keyword_name','status')->first();
            if(!$keyword){
                try{
                    $keyword = new ArticleKeywordWechat3();
                    $keyword->keyword_name = $keyword_name;
                    $keyword->created_by = $users->id;
                    $keyword->created_at = Common::currentTimeString();
                    $keyword->updated_by = $users->id;
                    $keyword->updated_at = Common::currentTimeString();
                    $keyword->status = 1;
                    $keyword->save();

                    $keyword = ArticleKeywordWechat3::where('keyword_name',$keyword_name)->select('id','keyword_name','status')->first();
                }catch(Exception $e){
                    Log::error("error", array("system" => "ahp3.0" ,"event" => "articleKeyword", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get('ahp_error.ahp_article_add_not_keyword_40009');
                    break;
                }
            }

            if($keyword->status != 1){
                $response = Lang::get('ahp_error.wechat3_article_status_over_keyword_40015');
                break;
            }

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $keyword);
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "articleKeyword", "input" =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    public function qrCode(){
        $size = Input::get('size');
        $text = Input::get('text');
        $type = Input::get('type',1);
        if(!$size || !$text) return '';
        $qrCode = new QrCode();
        $qrCode->setText($text."&type=".$type);
        $qrCode->setSize($size);
        $qrCode->setPadding(10);
        $response = Response::make($qrCode->get(), 200);
        $response->header('content-type', 'image/png');
        $response->header('Access-Control-Allow-Origin', '*');
        $response->header('Access-Control-Allow-Headers', 'X-Requested-With,X_Requested_With');
        $response->header('Access-Control-Allow-Headers', 'X-Requested-With, X_Requested_With');
        //$response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, Accept');
        $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
        $response->header('Access-Control-Allow-Credentials', 'true');
        return $response;
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-10-29 13:48:28
     * @Description: 文章--评论列表
     * @param  $token  String   用户token
     * @link:http://www.activity.com/ahp/ahp3.0/article/articleCommentList?token=3bf1ddae386db9708d429e3ac3641be2&article_id=14&page=1&page_size=100
     */
    public function articleCommentList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'article_id'=> 'required',
                'page'=> 'required',
                'page_size'=> 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $users = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $article_id = Input::get('article_id');
            $page = Input::get('page');
            $page_size = Input::get('page_size');

            //校验文章是否存在
            $article_frist = ArticleWechat3::where('id',$article_id)->where('status',1)->where('source','!=',0)->first();
            if(is_null($article_frist)){
                $response = Lang::get('error.wechat3_article_not_exist_20000');
                break;
            }
            //查询评论
            $article = ArticleCommentWechat3::where('article_id',$article_id)
                ->with('parent')
                ->orderBy('created_at','desc');
            //根据用户id查询
            if(Input::get('user_id')){
                $article = $article->where('user_id',Input::get('user_id'));
            }
            //根据审核状态查询
            if(Input::get('status')){
                $article = $article->where('status',Input::get('status'));
            }
            //根据置顶状态查询
            if(Input::get('sort')){
                $article = $article->where('sort',Input::get('sort'));
            }
            //根据违规状态查询
            if(Input::get('Irregularities')){
                $article = $article->where('Irregularities',Input::get('Irregularities'));
            }
            //根据用户类型查询
            if(Input::get('user_type')){
                $article = $article->where('user_type',Input::get('user_type'));
            }
            //根据评论内容模糊查询
            if(Input::get('content')){
                $article = $article->where('content','LIKE','%'.Input::get('content').'%');
            }
            $article->orderBy('created_at','desc')->get()->toArray();
            $count = $article->count();
            $article=$article->skip($page_size*($page-1))->take($page_size)->get();

            if(!empty($article)){
                foreach($article as $key => $val){
                    if($val['Irregularities'] == 1){
                        $article[$key]['content_show'] = Config::get('article_share_wechat3.article_Irregularities_message');
                    }
                    if($val['parent']['Irregularities'] == 1){
                        $article[$key]['parent']['content_show'] = Config::get('article_share_wechat3.article_Irregularities_message');
                    }
                }
                //统计评论数
                $comment = ArticleCommentWechat3::where('article_id',$article_id)->where('user_type','!=','2')->count();
            }

            $response = array('code' => 0, 'msg' => 'ok','result' => array('count' => $count, 'lists' => $article, 'comment' => $comment));
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "articleCommentList", "input" =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-10-29 13:48:28
     * @Description: 文章--评论审核、置顶、违规
     * @param  $token  String   用户token
     * @link:  http://www.activity.com/ahp/ahp3.0/article/articleCommentSave?token=a3aeb95a06b53760105d734b9123a3cf&id=[%201,%202,%203%20]&type=1&status=2
     */
    public function articleCommentSave(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=> 'required',
                'type'=> 'required', //type = 1 审核 2-置顶 3-违规
                'status'=> 'required',// 审核：1-已审核 2-未审核 ；置顶：1-是 2-否 ；违规：1-是 2-否 ；
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $users = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $id = Input::get('id');
            $type = Input::get('type');
            $status = Input::get('status');

            //校验评论是否存在
            //$id = json_decode($id);

            $article_frist = ArticleCommentWechat3::whereIn('id',$id)->first();
            if(is_null($article_frist)){
                $response = Lang::get('error.wechat3_article_comment_not_exist_error_20011');
                break;
            }

            if($type == 1){
                //审核
                $keyword = 'status';
            }elseif($type == 2){
                //置顶
                $keyword = 'sort';
            }elseif($type == 3){
                //违规
                $keyword = 'Irregularities';
            }else{
                $response = Lang::get('error.wechat3_article_caozuo_not_exist_error_20012');
                break;
            }

            $commet = ArticleCommentWechat3::whereIn('id',$id)->update([$keyword=>$status,'updated_at' => Common::currentTimeString()]);

            $response = array('code' => 0, 'msg' => 'ok');
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "articleCommentSave", "input" =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-10-29 13:48:28
     * @Description: 文章--评论官方回复
     * @param  $token  String   用户token
     * @link: http://www.activity.com/ahp/ahp3.0/article/articleCommentReply?token=a3aeb95a06b53760105d734b9123a3cf&id=[%201,%202,%203%20]&content=%E6%97%B6%E5%BA%86%E5%BA%86&article_id=14
     */
    public function articleCommentReply(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'article_id'=> 'required',
                'id'=> 'required',
                'content'=> 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $users = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $id = Input::get('id');
            $article_id = Input::get('article_id');
            $content = $str = Input::get('content');

            //校验文章是否存在
            $article_frist = ArticleWechat3::where('id',$article_id)->where('status',1)->where('source','!=',0)->first();
            if(is_null($article_frist)){
                $response = Lang::get('ahp_error.article_not_exit_error_8101');
                break;
            }
            $key = 'demand_sensitive_word';
            //$temp = Redis::del($key);die; //删除
            $temp = Redis::get($key);
            if(!$temp){
                $temp = DemandSensitiveWord::all()->toArray();
                $temp = base64_encode(json_encode($temp));
                Redis::setex( $key , 60*24*365, $temp );
            }

            $demand_sensitive_word = json_decode(base64_decode($temp),true);
            $badword = array();
            if(!is_null($demand_sensitive_word)){
                $badword = array_column($demand_sensitive_word,'badword');
            }
            if(!empty($badword)){
                $badword1 =array_combine($badword,array_fill(0,count($badword),'*'));
                $str = strtr($content,$badword1);
            }
            //$id = json_decode($id);
            if(!empty($id)){
                foreach($id as $_key){
                    $comment = ArticleCommentWechat3::commentAdd($_key,$content,$str,$users,$article_id,2,2);
                }
            }

            $response = array('code' => 0, 'msg' => 'ok');
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "articleCommentReply", "input" =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-10-29 13:48:28
     * @Description: 文章--官方评论
     * @param  $token  String   用户token
     * @link: http://www.activity.com/ahp/ahp3.0/article/articleComment?token=a3aeb95a06b53760105d734b9123a3cf&content=%E6%97%B6%E5%BA%86%E5%BA%86&article_id=14
     */
    public function articleComment(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'article_id'=> 'required',
                'content'=> 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $users = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $article_id = Input::get('article_id');
            $content = $str = Input::get('content');
            $id = Input::get('id');

            //校验文章是否存在
            $article_frist = ArticleWechat3::where('id',$article_id)->where('status',1)->where('source','!=',0)->first();
            if(is_null($article_frist)){
                $response = Lang::get('ahp_error.article_not_exit_error_8101');
                break;
            }
            $key = 'demand_sensitive_word';
            //$temp = Redis::del($key);die; //删除
            $temp = Redis::get($key);
            if(!$temp){
                $temp = DemandSensitiveWord::all()->toArray();
                $temp = base64_encode(json_encode($temp));
                Redis::setex( $key , 60*24*365, $temp );
            }

            $demand_sensitive_word = json_decode(base64_decode($temp),true);
            $badword = array();
            if(!is_null($demand_sensitive_word)){
                $badword = array_column($demand_sensitive_word,'badword');
            }
            if(!empty($badword)){
                $badword1 =array_combine($badword,array_fill(0,count($badword),'*'));
                $str = strtr($content,$badword1);
            }
            $comment = ArticleCommentWechat3::commentAdd($id,$content,$str,$users,$article_id,2,2);

            $response = array('code' => 0, 'msg' => 'ok');
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "articleComment", "input" =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-10-29 13:48:28
     * @Description: 文章--关键词列表
     * @param  $token  String   用户token
     * @link:http://www.activity.com/ahp/ahp3.0/article/keyWordList?token=3bf1ddae386db9708d429e3ac3641be2&page=1&page_size=10&keyword=%E9%80%89%E6%8B%A9
     */
    public function keyWordList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'page'=> 'required',
                'page_size'=> 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $users = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $page = Input::get('page');
            $page_size = Input::get('page_size');

            $keyword = ArticleKeywordWechat3::with('ahp_user');
            //根据审核状态查询
            if(Input::get('status')){
                $keyword = $keyword->where('status',Input::get('status'));
            }
            //根据精选状态查询
            if(Input::get('is_recommend')){
                $keyword = $keyword->where('is_recommend',Input::get('is_recommend'));
            }
            //根据评论内容模糊查询
            if(Input::get('keyword')){
                $keyword = $keyword->where('keyword_name','LIKE','%'.Input::get('keyword').'%');
            }
            $keyword->orderBy('created_at','desc')->get()->toArray();
            $count = $keyword->count();
            $keyword=$keyword->skip($page_size*($page-1))->take($page_size)->get();

            if(!is_null($keyword)){
                foreach($keyword as $key =>$val){
                    $val->num = ArticleKeyWordRelationWechat3::where('keyword_id',$val->id)->count();
                }
            }

            $response = array('code' => 0, 'msg' => 'ok','result' => array('count' => $count, 'lists' => $keyword));
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "keyWordList", "input" =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-10-29 13:48:28
     * @Description: 文章--关键词状态修改、精选
     * @param  $token  String   用户token
     * @link:  http://www.activity.com/ahp/ahp3.0/article/articleKeyWordSave?token=a3aeb95a06b53760105d734b9123a3cf&id=[%201,%202,%203%20]&type=1&status=2
     */
    public function articleKeyWordSave(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=> 'required',
                'type'=> 'required', //type = 1 状态 2-精选
                'status'=> 'required',// 状态：1-开启 2-关闭 ；精选：1-是 2-否 ；
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $users = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $id = Input::get('id');
            $type = Input::get('type');
            $status = Input::get('status');

            //校验评论是否存在
            //$id = json_decode($id);

            $article_frist = ArticleKeywordWechat3::whereIn('id',$id)->first();
            if(is_null($article_frist)){
                $response = Lang::get('error.wechat3_article_comment_not_exist_error_20011');
                break;
            }

            if($type == 1){
                //审核
                $keyword = 'status';
            }elseif($type == 2){
                //置顶
                $keyword = 'is_recommend';
            }else{
                $response = Lang::get('ahp_error.wechat3_article_caozuo_not_exist_error_40016');
                break;
            }

            $commet = ArticleKeywordWechat3::whereIn('id',$id)->update([$keyword=>$status,'updated_at' => Common::currentTimeString(), 'updated_by' => $users->id]);

            $response = array('code' => 0, 'msg' => 'ok');
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "articleKeyWordSave", "input" =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

}